<template>
  <div>
    <form-marker
      :isEdit="isEditPage"
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData"
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'
import {isPositiveInteger} from '@/common/utils'
// import { Promise } from 'q';

export default {
  components: {
    FormMarker
  },
  data () {
    // 自定义验证规则
    let checkCode = (rule, value, callback) => {
      if (value && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.MAINDATA}/taxes/check`, {taxNo: value}).then((res) => {
          if (res.result) {
            // this.noRepeat = res.data
            if (!res.data) {
              callback(new Error('编码已存在，请重新输入'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        })
      } else {
        callback()
      }
    }
    let checkInt = (rule, value, callback) => {
      if (!isPositiveInteger(value)) {
        callback(new Error('请输入正整数'))
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '税率编码',
          name: '税率编码',
          prop: 'taxNo',
          isDisable: true
        },
        {
          type: 'input',
          label: '税率名称',
          name: '税率名称',
          prop: 'taxName'
        },
        {
          type: 'input',
          label: '税率值',
          name: '税率值',
          prop: 'value',
          iconEnd: true
        },
        // {
        //   type: 'select',
        //   label: '发票类型',
        //   name: '发票类型',
        //   prop: 'parentCode',
        //   propName: 'parentName',
        //   optionLabel: 'name',
        //   optionValue: 'code',
        //   joint: true,
        //   isDisable: 'parentCode'
        // },
        {
          type: 'input',
          label: '备注',
          name: '备注',
          prop: 'remark'
        }
      ],
      rules: {
        taxNo: [
          { max: 32, message: '长度不可超过32个字符', trigger: 'change' },
          { required: true, message: '税率编码不允许为空', trigger: 'change' },
          { validator: checkCode, trigger: 'change' }
        ],
        taxName: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' },
          { required: true, message: '税率名称不允许为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '税率值不允许为空', trigger: 'blur' },
          { validator: checkInt, trigger: 'blur' }
        ],
        remark: [
          { max: 255, message: '长度不可超过255个字符', trigger: 'blur' }
        ]
      },
      selectData: {},
      detailData: {},
      noRepeat: false, // 编码唯一性
      isEditPage: false // 编辑or新增
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    // this.getSelect()
    this.getTaxrateDetail()
  },
  methods: {
    // 获取下拉列表数据
    // async getSelect () {
    //   let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/taxes/listParentAreas`, {})
    //   this.selectData.parentCode = data
    //   this.selectData = JSON.parse(JSON.stringify(this.selectData))
    // },
    // 获取编辑详情数据
    async getTaxrateDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/taxes/getOne`, {id: this.pageType})
        this.detailData = data
        this.isEditPage = true
      }
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = this.pageType === 'new' ? `/${this.$const.PREFIX.MAINDATA}/taxes/save` : `/${this.$const.PREFIX.MAINDATA}/taxes/update`
      let params = val
      this.$http.post(url, params).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'taxrate'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'taxrate'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>